/**
 *
 */
package com.jdy.haoduoai.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoai.activity.ChoiceActivity;
import com.jdy.haoduoai.activity.ExpandListActivity;
import com.jdy.haoduoai.model.CategoryItem;
import com.jdy.haoduoai.model.RadioCategoryItem;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.List;

/**
 * @author zp 2016-4-14 下午2:46:46
 */
public class RadioCategoryGridViewAdapter extends BaseAdapter {

    private final Context mContext;

    public boolean isAddHabitMode() {
        return bAddHabitMode;
    }

    public void setAddHabitMode(boolean bAddHabitMode) {
        this.bAddHabitMode = bAddHabitMode;
    }

    private boolean bAddHabitMode;

    private final List<RadioCategoryItem> mRadioCategoryItemList;

    public RadioCategoryGridViewAdapter(Context ctx, List<RadioCategoryItem> list) {
        this.mContext = ctx;
        this.mRadioCategoryItemList = list;
    }

    @Override
    public int getCount() {
        if (this.mRadioCategoryItemList != null)
            return this.mRadioCategoryItemList.size();
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        if (this.mRadioCategoryItemList != null)
            return this.mRadioCategoryItemList.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final RadioCategoryItem rci = this.mRadioCategoryItemList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_grid_text, parent, false);
            holder = new ViewHolder();
            holder.root_view = convertView.findViewById(R.id.root_view);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.imgCover = (ImageView) convertView.findViewById(R.id.cover);
            holder.circleImg = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String path = "";

        if (!TextUtils.isEmpty(rci.icon)) {
            path = rci.icon;
        } else {
            path = mRadioCategoryItemList.get(position - 1).icon;
        }

        if (!TextUtils.isEmpty(path)) {
            Picasso
                    .with(mContext)
                    .load(path)
                    .placeholder(
                            mContext.getResources().getDrawable(R.drawable.default_cover))
                    .error(mContext.getResources().getDrawable(R.drawable.default_cover))
                    .into(holder.imgCover);
        }


        holder.title.setText(rci.name);

        if (!bAddHabitMode) {
            holder.root_view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (bAddHabitMode) {
                        CategoryItem categoryItem = convert2CI(rci);
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        intent.setClass(mContext, ExpandListActivity.class);
                        bundle.putSerializable("categoryitem", categoryItem);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    } else {
                        CategoryItem categoryItem = convert2CI(rci);
//                        if (categoryItem.name.equals("经典绘本")) {
//                            // Intent intent = new Intent();
//                            // Bundle bundle = new Bundle();
//                            // intent.setClass(mContext, IllustratedStoryActivity.class);
//                            // bundle.putSerializable("categoryitem", categoryItem);
//                            // intent.putExtras(bundle);
//                            // mContext.startActivity(intent);
//                        } else {
                            Intent intent = new Intent();
                            Bundle bundle = new Bundle();
                            intent.setClass(mContext, ChoiceActivity.class);
                            bundle.putSerializable("categoryitem", categoryItem);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
//                        }
                    }

                }
            });
        }


        return convertView;
    }

    private CategoryItem convert2CI(RadioCategoryItem rci) {
        CategoryItem ci = new CategoryItem();
        ci.id = rci.id;
        ci.name = rci.name;
        ci.code = rci.code;
        if (rci.enabled)
            ci.enabled = 1;
        else
            ci.enabled = 0;
        ci.sort = rci.sort;
        ci.created_at = rci.created_at;
        ci.updated_at = rci.updated_at;
        ci.deleted_at = rci.deleted_at;
        ci.icon = rci.icon;
        ci.catalog = rci.catalog;
        ci.home_page = rci.home_page;
        return ci;
    }

    private class ViewHolder {
        private TextView title;
        private ImageView imgCover;
        private ImageView circleImg;
        private View root_view;
    }

}
